package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 苏成博
 * @date 2023-02-12 18:48
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //判断数据库中是否有要添加的菜品或套餐数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findShoppingCart(shoppingCart);
        //如果数据库中有这条购物数据，那就修改数量
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            ShoppingCart shoppingCartTemp = shoppingCartList.get(0);
            shoppingCartTemp.setNumber(shoppingCartTemp.getNumber()+1);
            shoppingCartMapper.updateShoppingCart(shoppingCartTemp);
        }else {
            //如果数据库中没有这条购物数据,那就新增这条数据
            if (shoppingCart.getDishId()!=null){
                //如果是菜品那就新增菜品
                Dish dish = dishMapper.findById(shoppingCartDTO.getDishId().intValue());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
            }else {
                //否则是套餐新增套餐
                Setmeal setmeal = setmealMapper.findById(shoppingCartDTO.getSetmealId().intValue());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.saveShoppinCart(shoppingCart);
        }
    }

    /**
     * 查询购物车信息
     * @return
     */
    public List<ShoppingCart> listShoppingCart() {
        Long id = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList=shoppingCartMapper.listShoppingCart(id);
        return shoppingCartList;
    }

    /**
     * 删除购物车信息
     * @param shoppingCartDTO
     */
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.deleteOne(shoppingCart);
    }

    @Override
    public void deleteAll() {
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }
}




















